﻿using DAL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using EncryptHelper;
using System.Data;

namespace BLL
{
   public class UserBLL
    {
        #region 三层架构
        /// <summary>
        /// 执行离线查询
        /// </summary>
        /// <param name="where"></param>
        /// <param name="pams"></param>
        /// <returns></returns>
        public static CurrenUser GetServiceAll(string sql, params SqlParameter[] pams)
        {
            return UserDAL.GetServiceAll(sql, pams);
        }

        /// <summary>
        /// 获取登录账户和密码
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwad"></param>
        /// <returns></returns>
        public static CurrenUser GetUserAll(string name, string pwad)
        {
            return UserDAL.GetUserAll(name, pwad);
        }

        /// <summary>
        /// 根据id返回结果
        /// </summary>
        /// <param name="Userid"></param>
        /// <returns></returns>
        public static CurrenUser GetIDAll(int Userid)
        {
            return UserDAL.GetIDAll(Userid);
        }

        #endregion

        #region 三层架构.连线式
        UserDAL db = new UserDAL();
        /// <summary>
        /// 根据账号和密码登录
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public CurrenUser GetUserInfo(string Account, string Password)
        {
            //引用MD5加密
            Password = EncryptHelper.EncryptHelper.GetMD5Hash(Password);
            return db.GetUserInfo(Account, Password);
        }

        /// <summary>
        /// 根据id查询用户信息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public CurrenUser GetUserByID(int userid)
        {
            return db.GetUserByID(userid);
        }

        /// <summary>
        /// 多条件查询用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="phone"></param>
        /// <param name="sex"></param>
        /// <param name="startInTime"></param>
        /// <param name="EndInTime"></param>
        /// <returns></returns>
        public List<UserInfoQueryModel> GetUserList(UserInfoModel queryModel)
        {
            return db.GetUserList(queryModel);
        }
        #endregion

        #region 添加用户信息，如果为1添加成功，为0添加失败，为-1姓名不能重复
        /// <summary>
        /// 添加用户信息，如果为1添加成功，为0添加失败，为-1姓名不能重复
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int Add(UserAdd sql)
        {
            //引用MD5加密
           sql.Password = EncryptHelper.EncryptHelper.GetMD5Hash(sql.Password);
            //用户姓名不能重复
            if (db.GetName(sql.UserName))
            {
                return -1;
            }
            //定义添加信息，大于0添加成功，等于0添加失败
            int result = db.Add(sql);
            //添加成功
            if (result > 0)
            {
                return 1;
            }
            //添加失败
            else
            {
                return 0;
            }
            //return db.Add(sql);
        }
        #endregion

        #region 修改用户信息
        /// <summary>
        /// 根据id获取修改用户的信息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public UpdateUserInfoModel UpdateUserModelID(int userid)
        {
            return db.UpdateUserModelID(userid);
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int Update(UpdateUserInfoModel sql)
        {
            return db.Update(sql);
        }
        #endregion

        #region 删除信息
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="delete"></param>
        /// <returns></returns>
        public int DeleteUser(int id)
        {
            return db.DeleteUser(id);
        }

        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name = "sql" ></ param >
        /// < returns ></ returns >
        public int Updelete(int userid)
        {
            UpdateUserInfoModel u = db.GetdeleteDAL(userid);
            if (u.IsDelete)
            {
                return -1;
            }
            int re = db.Updelete(userid);
            if (re > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
            // return db.Updelete(userid);

        }
        #endregion

        #region 角色下拉框
        public DataTable juesedrop()
        {
            return db.juesedrop();
        }
        #endregion
    }
}
